package com.jy.appmarket.screenlock.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jy.appmarket.R;
import com.jy.appmarket.screenlock.model.AppModel;
import com.tonicartos.widget.stickygridheaders.SGHSimpleArrayAdapter;

public class MessageFilterAdapter extends SGHSimpleArrayAdapter<AppModel> {
	private LayoutInflater mInflater;

	public MessageFilterAdapter(Context context, List<AppModel> items) {
		super(context, items, R.layout.header_item, R.layout.message_filter_item);
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public long getHeaderId(int position) {
		AppModel item = getItem(position);
		String value = item.getFirst();
		return value.charAt(0);
	}

	@Override
	public View getHeaderView(int position, View convertView, ViewGroup parent) {
		AlphabetHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.header_item, parent, false);
			holder = new AlphabetHolder();
			holder.textView = convertView.findViewById(R.id.text);
			convertView.setTag(holder);
		} else {
			holder = (AlphabetHolder) convertView.getTag();
		}
		final AppModel item = getItem(position);
		holder.textView.setText(item.getFirst());
		return convertView;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		AppHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.message_filter_item, parent, false);
			holder = new AppHolder();
			holder.text = convertView.findViewById(R.id.app_name);
			holder.icon = convertView.findViewById(R.id.app_icon);
			holder.select = convertView.findViewById(R.id.shortcut_app_add);
			convertView.setTag(holder);

		} else {
			holder = (AppHolder) convertView.getTag();
		}

		final AppModel item = getItem(position);
		holder.icon.setImageDrawable(item.getIcon());
		holder.text.setText(item.getAppName());

		if (item.isSelected()) {
			holder.select.setVisibility(View.VISIBLE);
		} else {
			holder.select.setVisibility(View.GONE);
		}

		return convertView;
	}

	private class AlphabetHolder {
		public TextView textView;
	}

	private class AppHolder {
		private TextView text;
		private ImageView icon;
		private ImageView select;
	}

}
// view = LayoutInflater.from(parent.getContext()).inflate(
// R.layout.header_item, parent, false);
// } else {
// view = LayoutInflater.from(parent.getContext()).inflate(
// R.layout.choose_shortcut_gridview_item, parent, false);
